import { LoadPanel } from 'devextreme-react';
import React, { useEffect, useRef, useState } from 'react';
import { useModel } from 'umi';
import CpasLayout from '~/public/cpas-business/cpas-table/CpasLayout/index';

import SearchRenderD from '../components/search/SearchRenderD';
import { getAccountInfo } from '../product/YearAnalysis/server';
import { tableColumnsValuationTesting } from '../product/YearAnalysis/tableColumnsData';
import './index.less';
import TreeListPrice from './PriceSet';
import { updateComputerData } from './server';
const InventoryValuationTesting: React.FC = (props: any) => {
  const pageType = props.params?.type;
  const prodiffID = useRef<any>();
  const { initialState } = useModel('@@initialState');
  const ztModel: any = useModel('ztModel');
  const { ztDates } = ztModel;
  const [Load, setLoad] = useState(false);

  const cpasNotesRef = useRef<any>(null);
  const [treeListData, setTreeListData] = useState<any[]>();
  const { dbname, sjnd } = initialState?.curPart as any;
  const [tableData, setTtableData] = useState<any[]>();

  const searchRenderC = useRef<any>();
  const treeListPrice = useRef<any>();
  const [selectTreeNode, setSelectTreeNode] = useState<any>(null);

  const dataSorce = useRef<any[]>();
  const searchcallback = async () => {
    // setLoad(true);
    await updateComputer();
    // setLoad(false);
  };
  const updateComputer = async () => {
    let q = searchRenderC.current.getFormSearchValues();
    setLoad(true);
    let d = await updateComputerData(q[0], dbname);
    await getProductType({ endYear: q[0] });
    setLoad(false);
  };

  // 计价测试会调
  const setSummeryCallback = async (e: any, a: string, b: any) => {
    if (a === '年度单') {
      const q = searchRenderC.current.getFormSearchValues();
      await getProductType({ endYear: q[0] });
    } else {
      treeListPrice.current.showPop(treeListData);
      // InputRef.current.show({
      //   title: '商品计价方式设置',
      //   okCallback: () => {
      //     console.log('确定回调函数');
      //   },
      // });
    }
  };

  const TreeSearchcallback = ({ itemData }: any) => {
    if (itemData.name === '全部') {
      setTtableData(dataSorce.current);
      setSelectTreeNode(null);
    } else {
      setSelectTreeNode(itemData.id);
      setTtableData(dataSorce.current?.filter((a: any) => a.typeID === itemData.id));
    }
  };

  // 获取业务数据
  const getGoodsdata = async (id: any) => {
    setLoad(true);
    const dd: any = await getAccountInfo('Analysis.Inventory.CHJJ', { dbname: dbname, dataId: id });
    setLoad(false);

    if (dd.data.length) {
      dataSorce.current = dd.data;
      if (!selectTreeNode) {
        setTtableData(dd.data), console.log(dd.data, 'li w');
      } else {
        setTtableData(dd.data?.filter((a: any) => a.typeID === selectTreeNode));
      }
    }
  };

  // 获取商品类型
  const getProductType = async (a: any) => {
    await getGoodsdata(Number(a.endYear));
    const dd: any = await getAccountInfo('get.goods.type', { dbname: dbname });
    if (dd.data?.length) {
      setTreeListData(
        dd.data.map((a: any) => {
          return {
            ...a,
            typeCode: a.typeCode,
          };
        }),
      );

      // 构建列
    }
  };
  useEffect(() => {
    console.log(cpasNotesRef.current, 'li 11', searchRenderC.current);

    getProductType({
      dates: ztDates,
      startYear: ztDates.at(0),
      endYear: ztDates.at(-1),
    });
    // getTransactionType();
  }, []);

  function renderSearch() {
    return (
      <SearchRenderD
        yearDate={null}
        ref={searchRenderC}
        pageType={pageType}
        // Transaction_Type={Transaction_Type}
        setSummeryCallback={setSummeryCallback}
      />
    );
  }

  return (
    <div
      style={{
        border: '0px solid green',
        height: '100%',
        display: 'flex',
        flexDirection: 'column',
      }}
    >
      {treeListData?.length && (
        <>
          <CpasLayout
            id="employee"
            ref={cpasNotesRef}
            height={'400px'}
            searchText={'重算'}
            disabledHomeIcon={true}
            treeListData={treeListData}
            tableColumnsData={tableColumnsValuationTesting}
            tableData={tableData}
            renderSearch={renderSearch}
            searchClickCallback={searchcallback}
            excelName={pageType}
            isLeftTree={true}
            refreshBtnCallback={searchcallback}
            treeClickCallback={TreeSearchcallback}
            isJumpRowDetail={false}
            devGridProps={{
              id: 'redRow',
              keyExpr: 'productID',
              onContentReady: function (com: any) {
                if (com.component) {
                  com.component.selectRows(
                    dataSorce.current
                      ?.filter((a: any) => a.diff && Number(a.diff) !== 0)
                      .map((a: any) => a.productID),
                  );
                }
              },
            }}
          />
        </>
      )}
      <LoadPanel
        position={{ of: '#employee' }}
        visible={Load}
        showIndicator={true}
        shading={true}
        showPane={true}
      />
      <TreeListPrice ref={treeListPrice} updateComputer={updateComputer} />
    </div>
  );
};

export default InventoryValuationTesting;
